﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Windows.Forms;
using System.Diagnostics;

namespace WallpaperRandomizer
{

    public class NETFramework
    {
        private static List<string> installedVersions = null;

        public static List<string> AvailableVersions
           
        {
            get
            {
                if (installedVersions == null)
                {
                    installedVersions = new List<string>();
                    string componentsKeyName = "SOFTWARE\\Microsoft\\NET Framework Setup\\NDP";

                    RegistryKey componentsKey = Registry.LocalMachine.OpenSubKey(componentsKeyName);
                    string[] instComps = componentsKey.GetSubKeyNames();
                    foreach (string instComp in instComps)
                    {
                        RegistryKey key = componentsKey.OpenSubKey(instComp);
                        bool installed = (int)key.GetValue("Install") == 1;
                        if (installed)
                            installedVersions.Add(instComp);
                    }
                }
                return installedVersions;
            }
        }


        public static bool CheckForVersion(Version version)
        {
            switch (version)
            {
                case Version.Version1_1:
                    return AvailableVersions.Contains(Version1_1);
                case Version.Version2_0:
                    return AvailableVersions.Contains(Version2_0);
                case Version.Version3_0:
                    return AvailableVersions.Contains(Version3_0);
                case Version.Version3_5:
                    return AvailableVersions.Contains(Version3_5);
                default:
                    return false;
            }
        }


        public static bool DialogCheckForVersion(Version version)
        {
            switch (version)
            {
                case Version.Version1_1:
                    return DialogCheckForVersion(Version1_1);
                case Version.Version2_0:
                    return DialogCheckForVersion(Version2_0);
                case Version.Version3_0:
                    return DialogCheckForVersion(Version3_0);
                case Version.Version3_5:
                    return DialogCheckForVersion(Version3_5);
                default:
                    return false;
            }
        }


        public static bool DialogCheckForVersion(string version)
        {
            if (!AvailableVersions.Contains(version))
            {
                if (MessageBox.Show("This application needs a newer version of .NET Runtime, want to download it now?", ".NET Runtime " + version + " Required", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.Yes)
                {
                    ProcessStartInfo si = new ProcessStartInfo(DownloadUrl(version));
                    si.UseShellExecute = true;
                    Process.Start(si);
                }
                return false;
            }
            return true;
        }

        public static string DownloadUrl(string version) 
        {
            switch (version)
            {
                case Version1_1:
                    return Version1_1Url;
                case Version2_0:
                    return Version2_0Url;
                case Version3_0:
                    return Version3_0Url;
                case Version3_5:
                    return Version3_5Url;
            }
            return null;
        }

        public const string Version1_1 = "v1.1.4322";
        public const string Version2_0 = "v2.0.50727";
        public const string Version3_0 = "v3.0";
        public const string Version3_5 = "v3.5";

        public const string Version1_1Url = "http://www.microsoft.com/downloads/details.aspx?FamilyId=262D25E3-F589-4842-8157-034D1E7CF3A3&displaylang=en";
        public const string Version2_0Url = "http://www.microsoft.com/downloads/details.aspx?FamilyID=0856EACB-4362-4B0D-8EDD-AAB15C5E04F5&displaylang=en";
        public const string Version3_0Url = "http://www.microsoft.com/downloads/details.aspx?FamilyID=10CC340B-F857-4A14-83F5-25634C3BF043&displaylang=en";
        public const string Version3_5Url = "http://www.microsoft.com/downloads/details.aspx?FamilyId=333325FD-AE52-4E35-B531-508D977D32A6&displaylang=en";

        public static bool V11Installed { get { return AvailableVersions.Contains("v1.1.4322"); } }
        public static bool V20Installed { get { return AvailableVersions.Contains("v2.0.50727"); } }
        public static bool V30Installed { get { return AvailableVersions.Contains("v3.0"); } }
        public static bool V35Installed { get { return AvailableVersions.Contains("v3.5"); } }

        public enum Version
        {
            Version1_1,
            Version2_0,
            Version3_0,
            Version3_5
        }

    }
}
